package cn.webrx.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/checklogin")
public class CheckLogin extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");


        HttpSession session = req.getSession();

        //检查验证码
        String c1 = req.getParameter("check");
        String c2 = session.getAttribute("captcha").toString();
        if (c1.equalsIgnoreCase(c2)) {
            String a = req.getParameter("account");
            String p = req.getParameter("password");

            //账号有admin 密码有123 就是成功
            if (a.contains("admin") && p.contains("123")) {
                //登录成功
                System.out.println("登录成功");
                session.setAttribute("member", a);
                resp.sendRedirect("/member.jsp");
            } else {
                req.setAttribute("msg", "账号密码错误");
                //登录失败，账号密码不正确
                //resp.sendRedirect("login.jsp");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }

        } else {
            //验证码错误，请重新登录
            req.setAttribute("msg", "验证码错误");
            //resp.sendRedirect("login.jsp");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

    }
}
